package com.niit.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;


/**
 * @author hanli
 * @date 2025/3/3
 * @description TODO
 */
public class MybatisUtil {

    // 在内存中就保留一个就好了
    static SqlSessionFactory sqlSessionFactory;

    static{
        try {
            // 读取配置文件，创建输入流 inoutStream
            InputStream in  = Resources.getResourceAsStream("config/mybatis-config.xml");
            // 调用builder对象build方法，结合配置文件，创建sqlSessionFactory对象
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static SqlSession getSession(){
        // 建立连接
        return sqlSessionFactory.openSession();
    }
}